package Misc;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 /**
  * class for sending an mail -> used in registration
  * @author Rupert
  *
  */
public class SendMail {
 
    private String to;
    private String subject;
    private String text;
    private String from;
    private String password;
     
    public SendMail(String to, String subject, String text){
        this.to = to;
        this.subject = subject;
        this.text = text;
    }
     
    public void send() throws MessagingException{
    	
    	from = "examonlineapp@gmail.com";
    	password = "ExamOnlinePa$$";
         
    	Properties props = System.getProperties();
		props.put("mail.smtp.starttls.enable", "true");
    	props.put("mail.smtp.host", "smtp.gmail.com");
    	props.put("mail.smtp.user", from);
    	props.put("mail.smtp.password", password);
    	props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
    	props.put("mail.smtp.auth", "true");
    	
    	
    	Session session = Session.getInstance(props,
    			  new javax.mail.Authenticator() {
    				protected PasswordAuthentication getPasswordAuthentication() {
    					return new PasswordAuthentication(from, password);
    				}
    			  });
    	
        Message msg = new MimeMessage( session );
        InternetAddress addressFrom = new InternetAddress( from );
        msg.setFrom( addressFrom );
        InternetAddress addressTo = new InternetAddress( to );
        msg.setRecipient( Message.RecipientType.TO, addressTo );
        msg.setSubject( subject );
        msg.setContent( text, "text/plain" );
        Transport.send( msg );       
    }
}